問題描述
Wagtail Django‑form編輯現有對象 (Wagtail Django‑form edit existing object)
我正在按照 GitHub 上的 wagtail‑form‑example 將 Django 表單實現到 wagtail 中。除了能夠編輯現有提交之外,我已經完成了所有工作。
這是我到目前為止所遵循的代碼 https://github.com/gasman/wagtail‑form‑example/blob/master/flavours/models.py
我嘗試了以下幾種變體代碼。
def serve(request, id):
instance = get_object_or_404(MyModel, id=id)
form = MyForm(request.POST or None, instance=instance)
如果有幫助,我會收到以下消息:int() 參數必須是字符串、類似字節的對像或數字,而不是“builtin_function_or_method”
任何幫助/示例將不勝感激:)
參考解法
方法 1:
Changing def serve(self, request):
to def serve(request, id):
doesn't work that way.
Your second argument id
now holds the request
object and not the expected int like the error tells you.
Start by respecting the method arguments serve(self, request)
. That means you need to query your form in some other way.
Eg: get_object_or_404(MyModel, id=[some hardcoded id])
If you want any form to be shown as child of your page, you could use RoutablePageMixin add an url relative to your page. Eg:
@route(r'^(\d+)/$')
def form_view(self, request, form_id=None):
instance = get_object_or_404(MyModel, id=form_id)